package com.maegul.wui.components;

import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import com.maegul.dao.entities.MediaItem;
import com.maegul.wui.pages.PlayerPage;

public class MediaItemList extends Panel {

	private static final long serialVersionUID = 1L;

	/**
	 * Constructor de la clase, crea un panel el cual es conformado por una
	 * lista paginada por la cantidad de items especificados y la lista dada
	 * como parametro
	 * 
	 * @param id
	 *            el identificador del componente en el html
	 * @param list
	 *            la lista de MediaItems que se va a renderizar
	 * @param itmesPerPage
	 *            la cantidad de items que se desean poner por pagina.
	 */
	public MediaItemList(String id, List<? extends MediaItem> list,
			int itmesPerPage) {
		super(id);

		PageableListView<MediaItem> mItems = new PageableListView<MediaItem>(
				"medialist", list, itmesPerPage) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<MediaItem> item) {
				final MediaItem mItem = item.getModelObject();

				Image img = new Image("image", "thumb");
				// get source
				img.add(new AttributeModifier("src",
						new AbstractReadOnlyModel<String>() {

							private static final long serialVersionUID = 1L;

							@Override
							public String getObject() {

								// TODO arreglar que siempre tenga thumbnail
								// if (mItem.getThumbSource().equals("empty")) {
								// return mItem.getThumbSource();
								// } else
								return "/images/cover_unavailable.png";
							}
						}));

				img.setOutputMarkupId(true);
				item.add(img);

				item.add(new Label("name", mItem.getName()));
				item.add(new MultiLineLabel("description", mItem
						.getDescription()));
				item.add(new Label("price", " - $" + mItem.getPrice()));

				item.add(new Link<MediaItem>("buy", item.getModel()) {

					private static final long serialVersionUID = 1L;

					@Override
					public void onClick() {
						MediaItem selected = (MediaItem) getModelObject();
						// TODO redirigir a la pagina de checkOut MaegulSession.get().getCart().add(selected);
						
						PageParameters params = new PageParameters();
						params.add("id", selected.getId());
						
						setResponsePage(PlayerPage.class, params);
					}

				});
			}
		};

		add(mItems);
		add(new PagingNavigator("navigator", mItems));
	}

}
